<?php

namespace App\Http\Service\Douban;

use App\Models\Doubanpostimage;
use App\Http\Service\BaseService;

class DoubanpostimageService  extends BaseService
{
    private $model;

    public function __construct(Doubanpostimage $model)
    {
        $this->model = $model;
       
    }
   
    /**
     *
     * @param array $params
     * @return array
     */
    public function addDate($request,$data=[])
    {
        $post_id=$data['post_id'];
        $unique_id=$data['unique_id'];
        $new_data=[];
        $is_cover=2;
        foreach ($data['photos_url'] as $k=>$v){
            $i=$k+1;
            if($i=='1'){
                $is_cover=1;
            }
            
            $new_data[]=[
                'url'=>$v,
                'sort_id'=>$i,
                'post_id'=>$post_id,
                'is_cover'=>$is_cover,
                'unique_id'=>$unique_id,
                'create_time'=>date("Y-m-d H:i:s"),
                'update_time'=>date("Y-m-d H:i:s"),
            ];
        }
        $info=$this->model->where(['unique_id'=>$unique_id])->first();
        if($info){
            #删除数据
            $this->model->where(['unique_id'=>$unique_id])->delete();
        }
        $res=$this->model->insert($new_data);
        
        
    }
}
